/*
 * ©2023 JOY-HUBS.COM
 */

package com.joyhubs.base.config;

import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import lombok.RequiredArgsConstructor;

import com.joyhubs.base.advice.ContentBodyMessageConverter;

/**
 * web configuration
 * <p/>
 * {@link EnableWebMvc} is enabled by swagger-ui
 */
@EnableWebMvc
@Configuration
@ConditionalOnBean(JoyWebAutoConfiguration.class)
@RequiredArgsConstructor
public class JoyWebMvcConfig implements WebMvcConfigurer {
    
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // add jackson2 http converter in the top of converter list, support string return type http request
        converters.add(0, contentBodyConverter());
    }
    
    @Bean
    public MappingJackson2HttpMessageConverter contentBodyConverter() {
        ContentBodyMessageConverter converter = new ContentBodyMessageConverter();
        return converter;
    }
    
}
